package com.zzxy.mybatis.tset;

import com.zzxy.mybatis.dao.UserDaoImpl;
import com.zzxy.mybatis.entity.Order;
import com.zzxy.mybatis.entity.Orderdetail;
import com.zzxy.mybatis.entity.User;
import com.zzxy.mybatis.mapper.OrderMapper;
import com.zzxy.mybatis.mapper.UserMapper;
import com.zzxy.mybatis.vo.QueryVO;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Test {
    SqlSession sqlSession;
    @Before
    public void before() throws IOException {
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is);
        sqlSession=ssf.openSession();
    }
    @org.junit.Test
    public void test() throws IOException {

    /*    User user=sqlSession.selectOne("findUserById",1);
        System.out.println(user);*/
     /*   List<User> users = sqlSession.selectList("findUserByName", "张");
        for (User user:users){
            System.out.println(user);
        }*/
   /*     User user=new User();
        user.setUsername("哈哈");
        user.setBirthday(new Date());
        user.setAddress("枣庄");
        user.setSex("1");
        sqlSession.insert("insertUser",user);
        sqlSession.commit();*/
      /*  User user=new User();
        user.setId(1);
        user.setUsername("王六");
        user.setSex("2");
        sqlSession.update("updateUser",user);
        sqlSession.commit();*/
     /*   sqlSession.delete("deleteUser",28);
        sqlSession.commit();
        sqlSession.close();*/

    }

    @org.junit.Test
    public void test2(){
            UserDaoImpl dao=new UserDaoImpl(sqlSession);
            User user=dao.findUserByDao(1);
            System.out.println(user);
    }
    @After
    public void after(){
        sqlSession.close();
    }

    @org.junit.Test
    public void test3(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    /*  User user=mapper.findUserById(1);
        System.out.println(user);*/
       QueryVO vo=new QueryVO();
        User user=new User();
        user.setId(1);
        Order order=new Order();
    //    order.setNumber("1000010");

        vo.setUser(user);
        vo.setOrder(order);

        List<User>users=mapper.findUserByVo(vo);
        for (User user1 : users){
            System.out.println(user1);
        }
     /*   HashMap<String,String> map=new HashMap<>();
        map.put("username","张三丰");
        map.put("address","河南郑州");
        User userByMap=mapper.findUserByMap(map);
        System.out.println(userByMap);*/
    }
    @org.junit.Test
    public void test6(){
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    /*    QueryVO vo = new QueryVO();
        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(3);
        ids.add(5);
        ids.add(7);
        ids.add(10);

        List<User> users = mapper.findUserByList(ids);
        vo.setIds(ids);
     //   List<User> users=mapper.findUserByIds(vo);
        for (User user : users){
            System.out.println(user);
        }*/
        List<User> users=mapper.findUserByMore2More();
        System.out.println(users);
      /*  for (User user:users){
            for (Order order: user.getOrders()){
                for (Orderdetail od: order.getOds()){
                    System.out.println(od.getItem());
                }
            }
        }*/
    }
    @org.junit.Test
    public void tset7(){
        OrderMapper mapper=sqlSession.getMapper(OrderMapper.class);
    //    Order order=mapper.findOrderBy121(3);
        List<Order> orders = mapper.findOrderBy12More(3);
        System.out.println(orders);
    }
}
